<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
/**
 * 统一返回信息
 * @param $status
 * @param $data
 * @param $msg
 * @return array
 */
function msg($status, $data = '', $msg = '')
{
    return compact('status', 'data', 'msg');
}
/**
 * 替换特殊字符 去除空格
 * @param $value
 * @return string
 */
 function replace($value){
        $search = array(" ","　","\n","\r","\t");
        $replace = array("","","","","");
        $content = str_replace($search,$replace,$value);
        return $content;
}
/**
 * 生成随机数字
 * 
 */
function rands(){
    $time = strtotime(date('Y-m-d H:i:s'));
    $rand = substr($time,1,4).rand(100,999);
    return $rand;
}
function format_market_cap($val) {
    if ($val >= 1000000000) { // 10亿以上，到亿，不保留小数
        $val = round($val / 100000000) . "亿";
    } else if ($val >= 100000000) {// 及亿以上，到亿，保留2位小数
        $val = number_format(($val/100000000),2). "亿";
    } else if ($val >= 100000) { // 千万、百万、十万以上，到万，不保留小数
        $val = round($val/10000). "万";
    } else if ($val >= 10000) { // 万以上，保留4位小数；
        $val = number_format($val/10000,4)."万";
    } else if ($val >= 1000) {
        $val = $val;
    } else {
        $val = $val;
    }
    return $val;
}
/**
 * 短信账户密码
 * 
 */
function userpassword(){
    $data['user'] = 'xinliankeji888';
    $data['password'] = 'xinliankeji123';
    return $data;
}
/**
 * 获取树结构
 * @param $arr
 * @param $index
 * @return array
 */
function getTree($arr, $index)
{
    $tree = [];
    foreach ($arr as $k => $v) {
        if ($v[$index] != 0) {
            $arr[$v[$index]]['children'][] = &$arr[$k];
       }else{
            $tree[] = &$arr[$k];
        }
    }
    return $tree;
}
     /**
     * 生成JSON数据返回值
     */
     function JSONReturn($result)
    {
        return json_encode($result,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
    }
/**
   
 * @param $data  要加密的字符串
    * @param $key   密钥
   
    * @return string
   
    */
   
   function encrypt($data, $key = 'encrypt')
   
  {
   
       $key = md5($key);
   
       $x = 0;
   
       $len = strlen($data);
   
       $l = strlen($key);
   
      $char = '';
  
       for ($i = 0; $i < $len; $i++) {
   
           if ($x == $l) {
  
               $x = 0;

           }
   
        $char .= $key{$x};
   
           $x++;
   
       }
   
       $str = '';
  
      for ($i = 0; $i < $len; $i++) {
  
          $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
  
      }
  
      return base64_encode($str);
  
  }

   /**
  
   * @param $data    要解密的字符串
  
   * @param $key     密钥
  
   * @return string
  
   */
  
  function decrypt($data, $key = 'encrypt')
  
  {
  
      $key = md5($key);
  
      $x = 0;
  
      $data = base64_decode($data);
  
     $len = strlen($data);
  
      $l = strlen($key);
  
      $char = '';
  
      for ($i = 0; $i < $len; $i++) {
  
          if ($x == $l) {
  
              $x = 0;
  
          }
  
          $char .= substr($key, $x, 1);
  
          $x++;
  
      }
  
      $str = '';
  
      for ($i = 0; $i < $len; $i++) {
  
          if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
 
             $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
 
         } else {
 
             $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
 
         }
 
     }
 
     return $str;
 
 }
